<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="成绩评分标准" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="achievementStandardId">
<!--              <a-input v-model="model.achievementStandardId" placeholder="请输入成绩评分标准"  ></a-input>-->
<!--              <a-select v-model="model.achievementStandardId" placeholder="请输入成绩评分标准"  style="width: 70%"-->
<!--                        :getPopupContainer="(target) => target.parentNode">-->
<!--                <a-select-option v-for="item in achAchievStandardList" :key="item.value">{{ item.label }}</a-select-option>-->
<!--              </a-select>-->
              <j-dict-select-tag type="list" v-model="model.achievementStandardId"  style="width: 70%"
                                 dictCode="ach_achievement_standard,achievement_standard_name,id" placeholder="请输入成绩评分标准" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="关联对象类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="relObjType">
<!--              <a-input v-model="model.relObjType" placeholder="请输入关联应用对象类型（试卷(默认)"  ></a-input>-->
              <j-dict-select-tag type="list" v-model="model.relObjType"  style="width: 70%"
                                 dictCode="achievement_rel_obj_type" placeholder="请输入关联应用对象类型" />
              <div style='color: #bf1212;font-size: 10px;'>关联对象类型：默认（试卷）</div>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="关联对象" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="relObjId">
<!--              <a-input v-model="model.relObjId" placeholder="请输入关联对象"  ></a-input>-->
              <a-select v-model="model.relObjId" placeholder="请输入关联对象(包括试卷)"   style="width: 70%"
                        :getPopupContainer="(target) => target.parentNode">
                <a-select-option v-for="item in achExaminationPaperList" :key="item.value">{{ item.label }}</a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="所属部门" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sysOrgCode">
<!--              <a-input v-model="model.sysOrgCode" placeholder="请输入所属部门编码"  ></a-input>-->
              <a-select v-model="model.sysOrgCode" placeholder="请选择所属部门"  style="width: 70%"
                        :getPopupContainer="(target) => target.parentNode">
                <a-select-option v-for="item in departDataList" :key="item.value">{{ item.label }}</a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="是否有效" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="paperStandardEffective">
              <j-dict-select-tag type="list" style="width: 70%" v-model="model.paperStandardEffective"
                                 dictCode="valid_status" placeholder="请选择是否有效" />
            </a-form-model-item>
          </a-col>
<!--          <a-col :span="24">-->
<!--            <a-form-model-item label="创建时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="createTime">-->
<!--              <j-date placeholder="请选择创建时间" v-model="model.createTime"  style="width: 100%" />-->
<!--            </a-form-model-item>-->
<!--          </a-col>-->
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'
  import { getDepartDataList } from '@api/commonAPI'
  import { getAchAchievStandardList, getAllAchExaminationPaperList } from '@api/achievementCommonAPI'

  export default {
    name: 'RelAchStandardDistributionForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 7 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 14 },
        },
        confirmLoading: false,
        validatorRules: {
        },
        url: {
          add: "/relachstandarddistribution/relAchStandardDistribution/add",
          edit: "/relachstandarddistribution/relAchStandardDistribution/edit",
          queryById: "/relachstandarddistribution/relAchStandardDistribution/queryById"
        },
        departDataList: [],
        achAchievStandardList: [],//评分标准
        achExaminationPaperList: [], //试卷
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
      this.getDepartData();
      this.getAchAchievStandardData();
      this.getAchExaminationPaperData();
    },
    methods: {
      add () {
        //获取当前登录用户信息，并获取部门
        let loginUserData = window.localStorage.getItem("pro__Login_Userinfo")
        loginUserData = JSON.parse(loginUserData)
        this.modelDefault.sysOrgCode = loginUserData.value.orgCode;//获取了部门
        this.modelDefault.paperStandardEffective = 1;//默认启用
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }

        })
      },
      async getDepartData() {
        let res = await getDepartDataList()
        this.departDataList = []
        res.result.forEach(item => {
          this.departDataList.push({
            label: item.departName,
            value: item.orgCode
          })
        })
      },
      //获取分配时下拉的评分标准数据
      async getAchAchievStandardData() {
        let res = await getAchAchievStandardList()
        this.achAchievStandardList = []
        res.result.records.forEach(item => {
          this.achAchievStandardList.push({
            label: item.achievementStandardName,
            value: item.id
          })
        })
      },
      //获取试卷
      async getAchExaminationPaperData() {
        let res = await getAllAchExaminationPaperList()
        this.achExaminationPaperList = []
        res.result.records.forEach(item => {
          this.achExaminationPaperList.push({
            label: item.examinationPaperName,
            value: item.id
          })
        })
      },
    }
  }
</script>